library(plotly)
library(tidyverse)
p <- ggplot(diamonds, aes(x = log(carat), y = log(price))) +
geom_hex(bins = 100)
ggplotly(p)
m <- highlight_key(mpg)
p <- ggplot(m, aes(displ, hwy)) + geom_point()
gg <- highlight(ggplotly(p), "plotly_selected")
crosstalk::bscols(gg, DT::datatable(m))
## Setting the `off` event (i.e., 'plotly_deselect') to match the `on` event (i.e., 'plotly_selected'). You can change this default via the `highlight()` function.
# load the diamonds dataset from the ggplot2 package
data(diamonds, package = "ggplot2")
# create three visualizations of the diamonds dataset
plot_ly(diamonds, x = ~cut)
## No trace type specified:
## Based on info supplied, a 'histogram' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#histogram
plot_ly(diamonds, x = ~cut, y = ~clarity)
## No trace type specified:
## Based on info supplied, a 'histogram2d' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#histogram2d
# create three visualizations of the diamonds dataset
plot_ly(diamonds, x = ~cut, color = ~clarity, colors = "Accent")
## No trace type specified:
## Based on info supplied, a 'histogram' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#histogram
# make annotaion of your own finding
library(ggforce)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_mark_hull(aes(filter = model == "corvette", label = model)) + # we retrieve the corvette data region
labs(
title = "Fuel economy from 1999 to 2008 for 38 car models",
caption = "Source: https://fueleconomy.gov/",
x = "Engine Displacement",
y = "Miles Per Gallon"
)
Version 2.0.0 of ggplot2 introduced a way for other R packages to implement custom geoms. Some great examples include: ggrepel, ggalt, ggraph, geomnet, ggmosaic and ggtern. Although the ggplotly() function translates most of the geoms bundled with the ggplot2 package, it has no way of knowing about the rendering rules for custom geoms. The plotly package does, however, provide 2 generic functions based on the S3 scheme that can leveraged to inform ggplotly() about these rules (Chambers 1992).50 To date, the ggmosaic and ggalt packages have taken advantage of this infrastructure to provide translations of their custom geoms to plotly.
library(ggalt)
## Registered S3 methods overwritten by 'ggalt':
## method from
## grid.draw.absoluteGrob ggplot2
## grobHeight.absoluteGrob ggplot2
## grobWidth.absoluteGrob ggplot2
## grobX.absoluteGrob ggplot2
## grobY.absoluteGrob ggplot2
getS3method("to_basic", "GeomXspline")
## function (data, prestats_data, layout, params, p, ...)
## {
## data <- data[order(data[["x"]]), ]
## prefix_class(data, "GeomPath")
## }
## <bytecode: 0x7fe38a5e3e40>
## <environment: namespace:plotly>
function(data, prestats_data, layout, params, p, ...) {
data <- data[order(data[["x"]]), ]
prefix_class(data, "GeomPath")
}
## function(data, prestats_data, layout, params, p, ...) {
## data <- data[order(data[["x"]]), ]
## prefix_class(data, "GeomPath")
## }
# example from `help(geom_xspline)`
set.seed(1492)
dat <- data.frame(
x = c(1:10, 1:10, 1:10),
y = c(
sample(15:30, 10),
2 * sample(15:30, 10),
3 * sample(15:30, 10)
),
group = factor(c(rep(1, 10), rep(2, 10), rep(3, 10)))
)
p <- ggplot(dat, aes(x, y, group = group, color = factor(group))) +
geom_point(color = "black") +
geom_smooth(se = FALSE, linetype = "dashed", size = 0.5) +
geom_xspline(spline_shape = 1, size = 0.5)
ggplotly(p) %>% hide_legend()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
# library(shiny)
#
# cities <- unique(txhousing$city)
#
# ui <- fluidPage(
# selectizeInput(
# inputId = "cities",
# label = NULL,
# # placeholder is enabled when 1st choice is an empty string
# choices = c("Please choose a city" = "", cities),
# multiple = TRUE
# ),
# plotlyOutput(outputId = "p")
# )
#
# server <- function(input, output, session, ...) {
# output$p <- renderPlotly({
# req(input$cities)
# if (identical(input$cities, "")) return(NULL)
# p <- ggplot(data = filter(txhousing, city %in% input$cities)) +
# geom_line(aes(date, median, group = city))
# height <- session$clientData$output_p_height
# width <- session$clientData$output_p_width
# ggplotly(p, height = height, width = width)
# })
# }
#shinyApp(ui, server)
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.